<template>
  <div class="container" style="margin: 10px">
    <el-card class="box-card">
      <div class="clearfix">
        <span style="font-width: 700">关键字</span>
        <el-input v-model="parameter.keyword" class="input-randoms" placeholder="请根据编号搜索" size="small"></el-input>
        <el-button style="float: right" type="primary" size="mini" @click="search">搜索</el-button>
        <el-button style="float: right; margin-right: 10px" size="mini" @click="parameter.keyword = ''">取消</el-button>
        <!-- 提示区域 -->
        <el-alert :title="`数据一共${counts}条`" type="info" show-icon :closable="false"> </el-alert>

        <!-- 表单区域 -->
        <template>
          <el-table :data="list" style="width: 100%" :header-cell-style="tableStyle" :row-style="trStyle">
            <el-table-column prop="id" label="编号" width="220"> </el-table-column>
            <el-table-column label="题型" width="80">
              <template #default="{ row }">
                {{ getOption(row.questionType) }}
              </template>
            </el-table-column>
            <el-table-column prop="questionIDs" label="题目编号" width="220">
              <template #default="{ row }">
                <el-link :underline="false" type="primary" v-for="item in row.questionIDs" :key="item.number" @click="seeTitle(item)">
                  {{ item.number }}
                </el-link>
              </template>
            </el-table-column>
            <el-table-column prop="addTime" label="录入时间" width="180"> </el-table-column>
            <el-table-column prop="totalSeconds" label="答题时间(s)" width="153"> </el-table-column>
            <el-table-column prop="accuracyRate" label="正确率(%)" width="151"> </el-table-column>
            <el-table-column prop="userName" label="录入人" width="151"> </el-table-column>
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button type="danger" circle plain icon="el-icon-delete" @click="remove(row)"> </el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <el-row type="flex" justify="end" style="margin-top: 15px">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="parameter.size"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="parameter.pagesize"
            layout="prev, pager, next,sizes,jumper"
            :total="counts"
          >
          </el-pagination>
        </el-row>
      </div>
    </el-card>

    <!-- 题目审查弹框 -->
    <preview :rousePreview="rousePreview" @closePreview="closePreview" :datas="datas" @close="rousePreview = false"></preview>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import { questionType } from '../../api/hmmm/constants'
import preview from '../components/questions-preview.vue'
export default {
  components: {
    preview
  },
  data() {
    return {
      tableStyle: {
        background: '#fafafa',
        borderBottom: '2px solid #e8e8e8'
      },
      trStyle: {
        height: '68px'
      },
      list: [],
      currentPage4: 1,
      //  页码 页数 关键字  接口的数据
      parameter: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      // 总条数
      counts: 0,
      // 弹出控制
      rousePreview: false,
      datas: {}
    }
  },
  methods: {
    // 每页多少条数据回调
    handleSizeChange(val) {
      this.parameter.pagesize = val
      this.getRandomList()
    },
    // 前进后退以及分页点击回调
    handleCurrentChange(val) {
      this.parameter.page = val
      this.getRandomList()
    },
    // 获取表单数据
    async getRandomList() {
      const { data: res } = await randoms(this.parameter)
      this.list = res.items
      this.counts = res.counts
      // console.log(res)
    },

    // 题型映射
    getOption(id) {
      const obj = questionType.find((item) => {
        return item.value == id
      })
      if (obj) return obj.label
      // console.log(this.type)
    },
    async search() {
      if (!this.parameter.keyword) return
      await this.getRandomList()
      this.parameter.keyword = ''
      console.log(1)
    },
    remove(id) {
      this.$confirm('是否确认删除数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await removeRandoms(id)
          this.getRandomList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    seeTitle(item) {
      this.rousePreview = true
      this.datas = item
      // console.log(this.datas)
    },
    closePreview(val) {
      this.rousePreview = val
    }
  },
  created() {
    this.getRandomList()
  }
}
</script>

<style scoped lang='scss'>
.input-randoms {
  width: 250px;
  margin-left: 10px;
  margin-bottom: 20px;
}
.el-alert {
  margin-bottom: 20px;
}
::v-deep .el-pagination__jump {
  margin-left: 0;
}
</style>
